from django.db import models


from files.models import File


REPOSITORY_TYPES = (
    ('D', 'Directory'),
    ('S', 'SVN'),
)     
    
class Repository(models.Model):
  name = models.CharField(max_length=256)
  type = models.CharField(max_length=1, choices=REPOSITORY_TYPES, default='S')
  url = models.CharField(max_length=256)
  path = models.CharField(max_length=256)
  
  stamp = models.DateTimeField(auto_now=True, auto_now_add=True, db_index=True)
  
  class Meta:
    db_table = 'Repositories'
    verbose_name_plural = "Repositories"
        
  def __unicode__(self):
    return self.name

  def scan(self):
    from assets.models import LOG
    from damn import ProviderInstance
    files = [0] #Meh pointers
    def Increase():
      files[0] += 1
    class CB():
      def addFile(self, fileRef):
        Increase()
        f,c = File.objects.get_or_create_from(fileRef)
        f.save()
        LOG.info('ADD F %s', fileRef.fileName)
      def deleteFile(self, path): pass
      def deleteDir(self, path): pass
      def updateFile(self, fileRef):
        Increase()
        f,c = File.objects.get_or_create_from(fileRef)
        f.save()
        LOG.info('UPDATE F %s - %s', fileRef.fileName, f.hash)
    ProviderInstance().scan(self.url, self.path, CB())
    LOG.info("Scanned %s files.", files[0])
